package org.sxp.common.controller;


import cn.hutool.core.util.StrUtil;
import com.xxl.job.admin.core.cron.CronExpression;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.sxp.common.web.WrapperResponse;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * job group controller
 * @author xuxueli 2016-10-02 20:52:56
 */
@Controller
@RequestMapping("/jobdesc")
public class JobDescController {

	@RequestMapping
	public String index(Model model) {
		return "jobdesc/jobdesc.index";
	}

	@PostMapping("/expression")
	@ResponseBody
	public WrapperResponse expression(String exp, int count) throws Exception{
		if(StrUtil.isBlank(exp)){
			return WrapperResponse.fail("请输入cron表达式");
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CronExpression cronExpression = new CronExpression(exp);
		List<String> plans = new ArrayList<>();
		Date now = new Date();
		for(int i=0;i<count;i++){
			now = cronExpression.getNextValidTimeAfter(now);
			plans.add(dateFormat.format(now));
		}
		return WrapperResponse.success(plans);
	}


}
